package com.guli.gulimall.member.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.guli.common.utils.HttpUtils;
import com.guli.common.vo.WeiboAccessVo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.member.dao.MemberDao;
import com.guli.gulimall.member.entity.MemberEntity;
import com.guli.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Autowired
    private MemberDao memberDao;

    @Override
    public MemberEntity weiboLogin(WeiboAccessVo vo) throws Exception {
        //判断是否注册过
        MemberEntity memberEntity = memberDao.selectOne(new QueryWrapper<MemberEntity>().eq("access_token", vo.getAccess_token()));
        if(memberEntity != null){
            //用户注册过
            memberEntity.setExpiresIn(vo.getExpires_in()+"");//更新过期时间
            memberEntity.setSocialUid(vo.getUid());
            //修改
            memberDao.updateById(memberEntity);
            return memberEntity;
        }else{
            //用户没有注册
            //远程调用微博登录接口
            HashMap<String, String> map = new HashMap<>();
            map.put("access_token",vo.getAccess_token());
            map.put("uid",vo.getUid());
            HttpResponse httpResponse = HttpUtils.doGet("https://api.weibo.com/2/users/show.json", "", "get", null, map);
            String s = EntityUtils.toString(httpResponse.getEntity());
            //获取到查询结果的json对象
            JSONObject jsonObject = JSON.parseObject(s);

            MemberEntity member = new MemberEntity();
            //微博信息放入
            member.setSocialUid(vo.getUid());
            member.setExpiresIn(vo.getExpires_in()+"");
            member.setAccessToken(vo.getAccess_token());

            //会员登录
            member.setLevelId(1L);
//            member.setUsername(jsonObject.get("name").toString());//用户名
//            member.setNickname(jsonObject.get("name").toString());//用户名
//            member.setHeader(jsonObject.get("profile_image_url").toString()); //头像
//            //性别
//            if("m".equals(jsonObject.get("gender"))){
//                member.setGender(0);
//            }else{
//                member.setGender(1);
//            }
            member.setIntegration(0);
            member.setGrowth(0);
            member.setStatus(1);
            member.setCreateTime(new Date());//当前时间
            //注册
            memberDao.insert(member);
            return member;
        }
    }

}